from app import create_app
import socket

def find_free_port(start_port=5000):
    """查找可用端口，从start_port开始"""
    port = start_port
    while True:
        with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as s:
            try:
                s.bind(('localhost', port))
                return port
            except OSError:
                port += 1
                continue

# 使用应用工厂创建应用实例
app = create_app()

if __name__ == '__main__':
    port = find_free_port()
    if port != 5000:
        print(f"Port 5000 is occupied, using port {port} instead.")
    app.run(debug=True, port=port)